<?php
/**
* Generate dynamic XSPF playlist by uploading mp3 files in a folder.
*
* Developed by Majid Khosravi
* http://www.majidkhosravi.com
* 
* This file is released under GNU GPL v3 license - http://www.gnu.org/licenses/gpl.html
*/

// Disable caching
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

// set file header to xml
header ("Content-Type:text/xml");

// Set this to the folder path where you keep your mp3 files
$music_dir = "music";

// allowed extensions in the playlist
$allowed_ext = array("mp3", "flv");

// get music absolute URL
$music_url = "http://".$_SERVER["HTTP_HOST"].substr($_SERVER["PHP_SELF"],0,strlen($_SERVER["PHP_SELF"])-12).$music_dir;


// Directory separator
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
	define('DS','\\');
}else{
	define('DS','/');
} 

// playlist starting tags
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<playlist version="0" xmlns = "http://xspf.org/ns/0/">';
echo '	<trackList>';

// if  music_dir is a directory
if (is_dir($music_dir)) 
{
	
	// read music folder contents
    if ($dh = opendir($music_dir)) 
    {
    	
    	
        while (($file = readdir($dh)) !== false) 
        {
        	// file path
        	$file_path = $music_dir .DS. $file;
        	
        	if (filetype($file_path) == "file")
        	{
        		
        		$file_info = pathinfo($file_path);
            	if( in_array($file_info["extension"], $allowed_ext))
            	{
            		echo '		<track>';
    				echo '			<location>'.$music_url."/".$file.'</location>';
    				echo '			<annotation>'.$file_info["filename"].'</annotation>';
   					echo '		</track>';
            	}
        	}
        }
        closedir($dh);
    }
}

echo '	</trackList>';
echo '</playlist>';
?>